﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ThinhTien.Models;
using ThinhTien.Data.Repository;

namespace ThinhTien.Admin.Controllers
{
    public class AccountController : Controller
    {
        private UserRepository user = new UserRepository();

        public ActionResult Login()
        {
            var model = new AccountViewModel()
            {
                Username = "developer",
                Password = "123456"
            };
            return View(model);
        }

        public ActionResult SignOut()
        {
            Session.Clear();
            Session.RemoveAll();
            Session.Abandon();
            return RedirectToAction("Login", "Account");
        }

        [HttpPost]
        public ActionResult Login(AccountViewModel model)
        {
            var userInfo = user.Login(model.Username, model.Password);
            if (userInfo != null)
            {
                Session["Username"] = userInfo.Username;

                Session["UserInfo"] = userInfo;
            }

            return RedirectToLocal(Url.Action("Index", "Home"));
        }

        private ActionResult RedirectToLocal(string returnUrl)
        {
            if (Url.IsLocalUrl(returnUrl))
            {
                return Redirect(returnUrl);
            }
            else
            {
                return RedirectToAction("Index", "Error");
            }
        }
    }
}
